#pragma once

#include <vector>
#include <thread>
#include <boost/asio.hpp>

namespace wsbus {

	class WorkerPool {
	public:
		explicit WorkerPool(std::size_t size)
			: workGuard_(boost::asio::make_work_guard(ioc_))
		{
			threads_.reserve(size);
			for (std::size_t i = 0; i < size; i++) {
				threads_.push_back(std::thread([this] { ioc_.run();}));
			}
		}

		~WorkerPool()
		{
			ioc_.stop();
			for (auto& t : threads_) {
				if (t.joinable()) {
					t.join();
				}
			}
		}

		template<class F>
		void post(F f)
		{
			boost::asio::post(ioc_, f);
		}

	private:
		std::vector<std::thread> threads_;
		boost::asio::io_context ioc_;
		typedef boost::asio::io_context::executor_type ExecutorType;
		boost::asio::executor_work_guard<ExecutorType> workGuard_;
	};

}